<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="handle" prop="expert_name">
                    <el-input v-model.trim="queryParams.expert_name" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="spu" prop="spu">
                    <el-input v-model="queryParams.spu" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="合作状态" prop="cooperate_status">
                    <el-select v-model="queryParams.cooperate_status" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in COOPERATION_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="平台" prop="platform_id">
                    <el-select v-model="queryParams.platform_id" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in PLATFORM_TYPE" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="合作方式" prop="cooperate_type">
                    <el-select v-model="queryParams.cooperate_type" placeholder="请选择" clearable style="width: 240px">
                        <el-option v-for="item in COOPERATION_TYPE" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="建联人" prop="fb_admin_id">
                    <SelectPage
                        v-model="queryParams.fb_admin_id"
                        placeholder="请选择"
                        :url="$api.purchase.getPurchaserList"
                        show_field="username"
                        show_id="operater_id"
                        query_field="keyword"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="物流状态" prop="fulfillment_status">
                    <el-select v-model="queryParams.fulfillment_status" placeholder="请选择" clearable style="width: 240px">
                        <el-option v-for="item in LOGISTICS_STATUS" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="合作日期" prop="cooperate_time">
                    <el-date-picker
                        v-model="queryParams.cooperate_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="是否超时" prop="is_out">
                    <el-select v-model="queryParams.is_out" placeholder="请选择" clearable style="width: 240px">
                        <el-option v-for="item in IS_TIMEOUT" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="发布时间" prop="publish_time">
                    <el-date-picker
                        v-model="queryParams.publish_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({}, 'add')">新增</el-button>
                </el-col>
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
                <el-col :span="1.5" style="float: right; margin-right: 48px">
                    <el-button type="primary" @click="showExpertData">{{ `达人合作看板` }}</el-button>
                </el-col>
            </el-row>
            <el-tabs v-model="queryParams.check_status" type="border-card" @tab-click="handleQuery">
                <el-tab-pane v-for="item in tabsList" :key="item.value" :label="item.label" :name="item.value.toString()"></el-tab-pane>
            </el-tabs>
            <div class="table-wrap">
                <el-table v-loading="loading" :data="tableData" border script>
                    <el-table-column label="达人ID" prop="expert_id" min-width="140" v-if="showColumn('expert_id')"></el-table-column>
                    <el-table-column label="handle" prop="expert_name" min-width="140" v-if="showColumn('expert_name')">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="gotoPersonalDetail(scope.row)">{{ scope.row.expert_name }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="主页" prop="link" min-width="200" v-if="showColumn('link')">
                        <template slot-scope="scope">
                            <el-tooltip :content="scope.row.link" placement="top">
                                <el-link v-if="scope.row.link" type="primary" :href="scope.row.link" target="_blank" class="ellipsis-2">{{ scope.row.link }}</el-link>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="合作日期" prop="cooperate_time" min-width="160" v-if="showColumn('cooperate_time')"></el-table-column>
                    <el-table-column label="店铺" prop="shop_name" min-width="120" v-if="showColumn('shop_name')"></el-table-column>
                    <el-table-column label="建联人" prop="operater" min-width="120" v-if="showColumn('operater')"></el-table-column>
                    <el-table-column label="平台" prop="platform_id" min-width="100">
                        <template slot-scope="scope">
                            {{ scope.row.platform_id | formatLabelByValueFuzzy(PLATFORM_TYPE) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="邮箱" prop="email" min-width="140" v-if="showColumn('email')"></el-table-column>
                    <el-table-column label="合作方式" prop="cooperate_type" min-width="120" v-if="showColumn('cooperate_type')">
                        <template slot-scope="scope">
                            {{ scope.row.cooperate_type | formatLabelByValueFuzzy(COOPERATION_TYPE) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="合作状态" prop="cooperate_status" min-width="120" v-if="showColumn('cooperate_status')">
                        <template slot-scope="scope">
                            {{ scope.row.cooperate_status | formatLabelByValueFuzzy(COOPERATION_STATUS) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="视频spu" prop="spu" min-width="140" v-if="showColumn('spu')"></el-table-column>
                    <el-table-column label="SKU" prop="sku" min-width="140" v-if="showColumn('sku')"></el-table-column>
                    <el-table-column label="物流状态" prop="fulfillment_status" min-width="100" v-if="showColumn('fulfillment_status')">
                        <template slot-scope="scope">
                            {{ scope.row.fulfillment_status | formatLabelByValueFuzzy(LOGISTICS_STATUS) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="是否超时" prop="is_out" min-width="100" v-if="showColumn('is_out')">
                        <template slot-scope="scope">
                            {{ scope.row.is_out | formatLabelByValueFuzzy(IS_TIMEOUT) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="合创码" prop="creation_code" min-width="160" v-if="showColumn('creation_code')"></el-table-column>
                    <el-table-column label="备注" prop="remarks" min-width="200" v-if="showColumn('remarks')">
                        <template slot-scope="scope">
                            <el-tooltip :content="scope.row.remarks" placement="top">
                                <span class="ellipsis-2">{{ scope.row.remarks }}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="发布链接" prop="publish_url" min-width="200" v-if="showColumn('publish_url')">
                        <template slot-scope="scope">
                            <el-tooltip :content="scope.row.publish_url" placement="top">
                                <el-link v-if="scope.row.publish_url" type="primary" :href="scope.row.publish_url" target="_blank" class="ellipsis-2">{{ scope.row.publish_url }}</el-link>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="超时日期" prop="expire_time" min-width="160" v-if="showColumn('expire_time')"></el-table-column>
                    <el-table-column label="发布日期" prop="publish_time" min-width="160" v-if="showColumn('publish_time')"></el-table-column>
                    <el-table-column label="创建日期" prop="create_time" min-width="160" v-if="showColumn('create_time')"></el-table-column>
                    <el-table-column label="操作" min-width="100" fixed="right">
                        <template slot-scope="scope">
                            <!-- <el-button el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">查看详情</el-button> -->
                            <div>
                                <el-button type="text" icon="el-icon-edit" style="padding: 4px 0" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                            </div>
                            <div>
                                <el-button type="text" icon="el-icon-delete" style="padding: 4px 0" @click="deleteRow(scope.row)">删除</el-button>
                            </div>
                            <div>
                                <el-button v-if="scope.row.check_status == 1" type="text" icon="el-icon-tickets" style="padding: 4px 0" @click="showAuditDialog(scope.row)">审核</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <el-dialog :title="title" :visible.sync="auditDialog.visible" width="420px" append-to-body>
            <el-form ref="form" :model="auditDialog.data" :rules="rules" inline label-width="100px" label-suffix=":">
                <el-form-item label="审核结果" prop="status">
                    <el-select v-model="auditDialog.data.status" placeholder="请选择" clearable size="small">
                        <el-option v-for="(dict, index) in approvalStatus" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" :loading="saveLoading" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getExpertCooperate, getShop, delExpertCooperate, checkExpert } from '@/api/expert'
import { APPROVAL_STATUS, TABS_LIST, COOPERATION_STATUS, PLATFORM_TYPE, COOPERATION_TYPE, LOGISTICS_STATUS, IS_TIMEOUT } from '../constant'
import DetailDialog from './components/DetailDialog.vue'
export default {
    name: 'CooperateList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 审批结果
            approvalStatus: APPROVAL_STATUS,
            tabsList: TABS_LIST,
            COOPERATION_STATUS, // 合作状态
            PLATFORM_TYPE, // 平台
            COOPERATION_TYPE, // 合作方式
            LOGISTICS_STATUS, // 物流状态
            IS_TIMEOUT, // 是否超时
            shopList: [], // 店铺列表
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                expert_name: undefined,
                shop_id: '',
                cooperate_status: undefined,
                fulfillment_status: undefined,
                cooperate_type: undefined,
                platform_id: undefined,
                fb_admin_id: undefined,
                is_out: undefined,
                create_time: [],
                publish_time: [],
                cooperate_time: [],
                check_status: '0',
            },
            detailDialog: {
                visible: false,
                data: {},
                type: '',
            },

            columns: [
                { label: '达人ID', prop: 'expert_id', visible: true },
                { label: 'handle', prop: 'expert_name', visible: true },
                { label: '主页', prop: 'link', visible: true },
                { label: '合作日期', prop: 'cooperate_time', visible: true },
                { label: '店铺', prop: 'shop_name', visible: true },
                { label: '建联人', prop: 'operater', visible: true },
                { label: '平台', prop: 'platform_id', visible: true },
                { label: '邮箱', prop: 'email', visible: true },
                { label: '合作方式', prop: 'cooperate_type', visible: true },
                { label: '合作状态', prop: 'cooperate_status', visible: true },
                { label: '视频spu', prop: 'spu', visible: true },
                { label: 'SKU', prop: 'sku', visible: true },
                { label: '物流状态', prop: 'fulfillment_status', visible: true },
                { label: '是否超时', prop: 'is_out', visible: true },
                { label: '合创码', prop: 'creation_code', visible: true },
                { label: '备注', prop: 'remarks', visible: true },
                { label: '发布链接', prop: 'publish_url', visible: true },
                { label: '超时日期', prop: 'expire_time', visible: true },
                { label: '发布日期', prop: 'publish_time', visible: true },
                { label: '创建时间', prop: 'create_time', visible: true },
            ],
            saveLoading: false,
            auditDialog: {
                visible: false,
                data: {
                    id: undefined,
                    status: undefined,
                },
            },
            rules: {
                status: [
                    { required: true, message: '请选择审核结果', trigger: 'blur' },
                ],
            },
            title: '审核',
        }
    },
    async created() {
        this.loading = true
        try {
            const result = await getShop()
            this.shopList = result?.data ?? []
        } catch (error) {
            console.log(error)
            this.loading = false
        }
        this.getList()
    },
    methods: {
        gotoPersonalDetail({expert_id}) {
            this.$router.push({
                path: '/kol/expertView',
                query: {
                    expert_id: expert_id,
                },
            })
        },
        showExpertData() {
            const shop_id = this.queryParams.shop_id || 17
            const url = window.location.origin + '/cooperateView' + '?shop_id=' + shop_id
            window.open(url, '_blank')
        },
        deleteRow(row) {
            this.$confirm('是否确认删除该数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delExpertCooperate({ id: row.id }).then(res => {
                        this.$message.success('删除成功')
                        this.handleQuery()
                    })
                })
                .catch(() => {})
        },
        submit() {
            this.saveLoading = true
            checkExpert(this.auditDialog.data)
                .then(() => {
                    this.auditDialog.visible = false
                    this.handleQuery()
                    this.$message.success('操作成功')
                })
                .finally(() => {
                    this.saveLoading = false
                })
        },
        cancel() {
            this.auditDialog.visible = false
        },
        showAuditDialog(row) {
            this.auditDialog.visible = true
            this.auditDialog.data.id = row.id
        },
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            exportOrder(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        showFeedbackDialog(row) {
            this.feedbackDialog.visible = true
            this.feedbackDialog.data = row
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }

            getExpertCooperate(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style>
.ellipsis-2 .el-link--inner {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
</style>
<style scoped>
.ellipsis-2 {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
